package com.wanhe.shoppingcart.manager;

import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.wanhe.base.BaseApplication;
import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.base.DishSize;
import com.wanhe.base.model.base.DishTag;
import com.wanhe.base.model.disgn.ViewItem;
import com.wanhe.base.model.holder.DishHolder;
import com.wanhe.base.util.NumberUtil;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.websocket.WebSocketTool;
import com.wanhe.base.websocket.model.BillDish;
import com.wanhe.shoppingcart.bean.BillDishResponse;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.bean.SetMealCartItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.R.attr.id;

/**
 * Created by XieZaiWei on 2017/5/9.
 */

public class CartManager implements StaticClassReleace {

	private static CartManager instance;

	/**
	 * 是加还是减
	 */
	private boolean									isAdd;
	private int										dishCount;
	private double									dishTotalCost;
	private double									vipDishTotalCost;
	/**
	 * 存放加入购物车的菜品
	 * key DishGuid value相同DishGuid菜品的集合, map的key是dish的guid+tag+size
	 */
	private Map <String, Map <String, ICartItem>>	cartItemStore	= new HashMap<>();
	/**
	 * 存放监听购物车变化的对象
	 */
	private List <ICartChangeObserver>				observers		= new ArrayList<>();
	private ICartItem								currentItem;

	private CartManager() {
		init();
	}

	public synchronized static CartManager getInstance() {
		if (ObjectUtil.isNull(instance)) {
			instance = new CartManager();
		}
		return instance;
	}

	/**
	 * 获取当前正在操作的
	 *
	 * @return
	 */
	public ICartItem getCurrentItem() {
		return currentItem;
	}

	private void init() {
		// 购物车初始化，每次进入到购物车界面都是空的，只有在点击购物项之后才加入到购物车
		calculate();
	}

	/**
	 * 添加购物车监听对象
	 *
	 * @param observer
	 */
	public void addCartObserver(ICartChangeObserver observer) {
		if (ObjectUtil.checkObject(observer)) {
			if (this.observers.contains(observer))
				return;
			this.observers.add(observer);
		}
	}

	/**
	 * 删除购物车监听对象
	 */
	public void removeObserver(ICartChangeObserver observer) {
		if (ObjectUtil.checkObject(observer)) {
			this.observers.remove(observer);
		}
	}

	/**
	 * 加的步长
	 *
	 * @param dishGuid
	 * @return
	 */
	public int getAddStep(String dishGuid) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		ViewItem.CountRule rule = dish.getCountRule();
		if (ObjectUtil.checkObject(rule)) {
			int itemCount = CartManager.getInstance().getSameGuidItemCount(dish.getDish_guid());
			if (itemCount == 0) {
				/**
				 * 第一次添加
				 * 
				 * @author 陈德基
				 * @time 2017/7/27 17:59
				 */
				return rule.getMin() == 0 ? rule.getStep() : rule.getMin();

			} else if (itemCount > rule.getMin() && itemCount < rule.getMax()) {
				/**
				 * 添加需要按照每次加多少数量进行添加
				 * 
				 * @author 陈德基
				 * @time 2017/7/27 18:00
				 */
				return rule.getStep();

			} else if (itemCount >= rule.getMax()) {
				/**
				 * 到达最高数量了，不能再加了，进行提示
				 * 
				 * @author 陈德基
				 * @time 2017/7/27 18:01
				 */
				Toast.makeText(BaseApplication.getApplicationComponent().getContext(),
						String.format("这道菜只能点%d份，点点其他菜吧~", rule.getMax()), Toast.LENGTH_SHORT)
						.show();
				return 0;

			} else {
				return 0;
			}

		} else {
			return 1;
		}

	}

	// 减的步长
	public int getReduceStep(String dishGuid) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		ViewItem.CountRule rule = dish.getCountRule();
		if (ObjectUtil.checkObject(rule)) {
			int itemCount = CartManager.getInstance().getSameGuidItemCount(dish.getDish_guid());
			if (itemCount == 0) {
				return 0;

			} else if (itemCount >= rule.getMin() + rule.getStep()) {

				return rule.getStep();

			} else if (itemCount <= rule.getMin()) {

				return rule.getMin();

			} else {
				return 0;
			}

		} else {
			return 1;
		}

	}

	public void addItem(String dishGuid, int count) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(dish.getDish_tagsList());
			boolean isSellOut = cartItem.isSellOut(count);
			if (!isSellOut) {
				addItemToCart(cartItem, count, true);
			} else {
				Toast.makeText(BaseApplication.context, "仅剩的" + cartItem.getName() + "都被你加完啦~",
						Toast.LENGTH_SHORT).show();
			}

		}
	}

	public void addItem(String dishGuid, int count, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(dish.getDish_tagsList());
			boolean isSellOut = cartItem.isSellOut(count);
			if (!isSellOut) {
				addItemToCart(cartItem, count, isNeedSyncToServer);

			} else {
				Toast.makeText(BaseApplication.context, "仅剩的" + cartItem.getName() + "都被你加完啦~",
						Toast.LENGTH_SHORT).show();
			}

		}
	}

	public void addItemByTag(String dishGuid, int count, List <DishTag> arrDishTag) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(arrDishTag);
			boolean isSellOut = cartItem.isSellOut(count);
			if (!isSellOut) {
				addItemToCart(cartItem, count, true);

			} else {
				Toast.makeText(BaseApplication.context, "仅剩的" + cartItem.getName() + "都被你加完啦~",
						Toast.LENGTH_SHORT).show();
			}

		}
	}

	public void addItemByTag(String dishGuid, int count, List <DishTag> arrDishTag,
			boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(arrDishTag);
			boolean isSellOut = cartItem.isSellOut(count);
			if (!isSellOut) {
				addItemToCart(cartItem, count, isNeedSyncToServer);

			} else {
				Toast.makeText(BaseApplication.context, "仅剩的" + cartItem.getName() + "都被你加完啦~",
						Toast.LENGTH_SHORT).show();
			}

		}
	}

	public void addItem(ICartItem item, int count) {
		boolean isSellOut = item.isSellOut(count);
		if (!isSellOut) {
			addItemToCart(item, count, true);
		} else {
			Toast.makeText(BaseApplication.context, "仅剩的" + item.getName() + "都被你加完啦~",
					Toast.LENGTH_SHORT).show();
		}
	}

	public void addItem(ICartItem item, int count, boolean isNeedSyncToServer) {
		boolean isSellOut = item.isSellOut(count);
		if (!isSellOut) {
			addItemToCart(item, count, isNeedSyncToServer);
		} else {
			Toast.makeText(BaseApplication.context, "仅剩的" + item.getName() + "都被你加完啦~",
					Toast.LENGTH_SHORT).show();
		}
	}

	public void addItem(BillDishResponse billDishResponse) {
		String strAmount = billDishResponse.getAmount();
		double amount = Double.valueOf(strAmount);
		if (amount > 0) {
			String dishGuid = billDishResponse.getDishID();
			Dish dish = DishHolder.getInstance().get(dishGuid);
			if (ObjectUtil.checkObject(dish)) {
				if (dish.isSetmeal()) {
					if(amount > 1){
						for (int i = 0; i < amount; i++) {
							addSetMeal(billDishResponse, dish, 1);
						}

					}else{
						addSetMeal(billDishResponse, dish, amount);
					}

				} else {
					CartItem cartItem = new CartItem();
					cartItem.setTags(billDishResponse.getTag());
					cartItem.setDish(dish);
					boolean isSellOut = cartItem.isSellOut((int) amount);
					if (!isSellOut) {
						addItemToCart(cartItem, (int) amount, false);
					}
				}
			}

		}

	}

	private void addSetMeal(BillDishResponse billDishResponse, Dish dish, double amount){
		SetMealCartItem setMealCartItem = new SetMealCartItem();
		setMealCartItem.setDish(dish);
		List <Dish> arrDish = dish.getChildDishList();
		// 对返回的套餐数据里套餐列表进行遍历，ID和本地缓存的ch_packdishuuid相等表示是选择的
		for (int i = 0; i < billDishResponse.getSetMealDish().size(); i++) {
			BillDish billDish = billDishResponse.getSetMealDish().get(i);
			String dishGuid1 = billDish.getDishID();
			for (int j = 0; j < arrDish.size(); j++) {
				Dish dish1 = arrDish.get(j);
				String guid = dish1.getDish_guid();
				if (guid.equals(dishGuid1)) {
					CartItem cartItem = new CartItem();
					cartItem.setDish(dish1);
					cartItem.setCount(1);
					setMealCartItem.addChildItem(cartItem);
					break;
				}
			}
		}
		boolean isSellOut = setMealCartItem.isSellOut((int) amount);
		if (!isSellOut) {
			addItemToCart(setMealCartItem, (int) amount, false);
		}
	}

	private void addItemToCart(ICartItem item, int addCount, boolean isNeedSyncToServer) {
		if (!ObjectUtil.checkObject(item)) {
			return;
		}
		ICartItem cartItem = null;
		Logger.d("=====addItem====" + item.getDishGuid() + "=isNeedSyncToServer===="
				+ isNeedSyncToServer);
		if (cartItemStore.containsKey(item.getDishGuid())) {
			Map <String, ICartItem> tMap = cartItemStore.get(item.getDishGuid());
			if (tMap != null) {
				cartItem = tMap.get(item.getSignID());
				if (cartItem != null) {
					int count = cartItem.getCount();
					count = count + addCount;
					cartItem.setCount(count);
				} else {
					item.setAddTime(System.currentTimeMillis());
					item.setCount(addCount);
					tMap.put(item.getSignID(), item);
					cartItem = item;
				}
			} else {
				item.setAddTime(System.currentTimeMillis());
				item.setCount(addCount);
				tMap = new HashMap<>();
				tMap.put(item.getSignID(), item);
				cartItemStore.put(item.getDishGuid(), tMap);
				cartItem = item;
			}
		} else {
			item.setAddTime(System.currentTimeMillis());
			item.setCount(addCount);
			Map <String, ICartItem> tMap = new HashMap<>();
			tMap.put(item.getSignID(), item);
			cartItemStore.put(item.getDishGuid(), tMap);
			cartItem = item;
		}
		currentItem = cartItem;
		this.isAdd = true;
		updateAndSend();

		// 同步到服务端
		if (isNeedSyncToServer) {
			BillDish billDish = convertToBillDish(item);
			billDish.setAmount(addCount);
			WebSocketTool.getInstance().shopCart(billDish);
		}

	}

	public void reduceItem(String dishGuid, int count) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(dish.getDish_tagsList());
			reduceItemCount(cartItem, count, true);
		}
	}

	public void reduceItem(String dishGuid, int count, boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(dish.getDish_tagsList());
			reduceItemCount(cartItem, count, isNeedSyncToServer);
		}
	}

	public void reduceItemByTag(String dishGuid, int count, List <DishTag> arrTag) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(arrTag);
			reduceItemCount(cartItem, count, true);
		}
	}

	public void reduceItemByTag(String dishGuid, int count, List <DishTag> arrTag,
			boolean isNeedSyncToServer) {
		Dish dish = DishHolder.getInstance().get(dishGuid);
		if (ObjectUtil.checkObject(dish)) {
			CartItem cartItem = new CartItem();
			cartItem.setDish(dish);
			cartItem.setTags(arrTag);
			reduceItemCount(cartItem, count, isNeedSyncToServer);
		}
	}

	public void reduceItem(ICartItem item, int count) {
		reduceItemCount(item, count, true);
	}

	public void reduceItem(ICartItem item, int count, boolean isNeedSyncToServer) {
		reduceItemCount(item, count, isNeedSyncToServer);
	}

	private void reduceItemCount(ICartItem item, int dishCount, boolean isNeedSyncToServer) {
		if (!ObjectUtil.checkObject(item)) {
			return;
		}
		ICartItem t = null;
		Map <String, ICartItem> map = cartItemStore.get(item.getDishGuid());
		if (!ObjectUtil.checkMap(map)) {
			return;
		}
		t = map.get(item.getSignID());
		if (t == null) {
			return;
		}
		int count = t.getCount();
		count = count - dishCount;

		if (count <= 0) {
			deleteItem(t);

		} else {
			t.setCount(count);
			this.isAdd = false;
			this.currentItem = t;
			updateAndSend();
		}

		// 同步到服务端
		if (isNeedSyncToServer) {
			BillDish billDish = convertToBillDish(item);
			billDish.setAmount(-dishCount);
			WebSocketTool.getInstance().shopCart(billDish);
		}
	}

	/**
	 * 删除购物项目
	 *
	 * @param item
	 *            购物项目
	 */
	public void deleteItem(ICartItem item) {
		if (!ObjectUtil.checkObject(item))
			return;
		if (cartItemStore.containsKey(item.getDishGuid())) {
			Map <String, ICartItem> tMap = cartItemStore.get(item.getDishGuid());
			if (tMap != null) {
				int count = tMap.size();
				if (count > 1) {
					currentItem = tMap.remove(item.getSignID());
				} else {
					Map <String, ICartItem> removed = cartItemStore.remove(item.getDishGuid());
					for (Map.Entry <String, ICartItem> entry : removed.entrySet()) {
						currentItem = entry.getValue();
					}
				}
			}
		}
		this.isAdd = false;
		updateAndSend();
	}

	public void deleteItem(String dishGuid) {
		if (!ObjectUtil.checkObject(dishGuid))
			return;

		ICartItem cartItem = null;
		Map <String, ICartItem> map = cartItemStore.get(dishGuid);
		if (!ObjectUtil.checkMap(map))
			return;
		for (Map.Entry <String, ICartItem> entry : map.entrySet()) {
			cartItem = entry.getValue();
		}
		if (cartItem == null)
			return;

		if (cartItemStore.containsKey(dishGuid)) {
			Map <String, ICartItem> tMap = cartItemStore.get(dishGuid);
			if (tMap != null) {
				int count = tMap.size();
				if (count > 1) {
					currentItem = tMap.remove(cartItem.getSignID());
				} else {
					Map <String, ICartItem> removed = cartItemStore.remove(dishGuid);
					for (Map.Entry <String, ICartItem> entry : removed.entrySet()) {
						currentItem = entry.getValue();
					}
				}
			}
		}
		this.isAdd = false;
		updateAndSend();
	}

	private void updateAndSend() {
		calculate();
		if (ObjectUtil.checkObjectList(observers)) {
			for (ICartChangeObserver observer : observers) {
				if (observer.isNeedUpdate()) {
					observer.onCartChanged(isAdd);
				}
			}
		}
	}

	/**
	 * 将购物车对象转换成与服务端同步的对象
	 */
	public BillDish convertToBillDish(ICartItem item) {
		BillDish billDish = new BillDish();
		if (item instanceof CartItem) {
			CartItem cartItem = (CartItem) item;
			billDish.setDishID(cartItem.getDishGuid());
			// billDish.setDishID("76258687-0c89-45ec-9414-53a15c1bac78");
			DishSize dishSize = cartItem.getSize();
			if (dishSize != null && dishSize.getName() != null) {
				billDish.setDishSizeName(cartItem.getSize().getName());
			} else {
				billDish.setDishSizeName(cartItem.getDish().getVch_unitname());
			}
			billDish.setName(cartItem.getName());
			billDish.setPrice(cartItem.getDish().getNum_price1() + "");
			billDish.setTag(cartItem.getTags());
			billDish.setComment(cartItem.getRemark());
			if (cartItem instanceof SetMealCartItem) {
				SetMealCartItem setMealCartItem = (SetMealCartItem) cartItem;
				billDish.setSetMealDish(convertToChildBillDish(setMealCartItem));
			}

		}

		return billDish;

	}

	/**
	 * 将购物车套餐对象转换成与服务端同步的对象
	 */
	private List <BillDish> convertToChildBillDish(SetMealCartItem setMealCartItem) {
		List <BillDish> arrChildDish = new ArrayList<>();
		List <CartItem> arrCartItem = setMealCartItem.getArrChildItem();
		for (int i = 0; i < arrCartItem.size(); i++) {
			BillDish billDish = new BillDish();
			CartItem cartItem = arrCartItem.get(i);
			billDish.setID(cartItem.getDish().getCh_packdishuuid());
			billDish.setDishID(cartItem.getDishGuid());
			DishSize dishSize = cartItem.getSize();
			if (dishSize != null && dishSize.getName() != null) {
				billDish.setDishSizeName(cartItem.getSize().getName());
			} else {
				billDish.setDishSizeName(cartItem.getDish().getVch_unitname());
			}
			billDish.setName(cartItem.getName());
			billDish.setPrice(cartItem.getDish().getNum_price1() + "");
			billDish.setTag(cartItem.getTags());
			billDish.setComment(cartItem.getRemark());
			billDish.setAmount(cartItem.getCount());
			arrChildDish.add(billDish);
		}

		return arrChildDish;

	}

	private void calculate() {
		dishCount = 0;
		dishTotalCost = 0;
		if (ObjectUtil.checkMap(cartItemStore)) {
			for (Map.Entry <String, Map <String, ICartItem>> entry : cartItemStore.entrySet()) {
				for (Map.Entry <String, ICartItem> tEntry : entry.getValue().entrySet()) {
					dishCount += tEntry.getValue().getCount();
					dishTotalCost += tEntry.getValue().getPrice() * tEntry.getValue().getCount();
				}
			}
		}
	}

	public boolean isSellOut(String key) {
		return cartItemStore.containsKey(key);
	}

	/**
	 * 判断商品是否已经在购物车中
	 *
	 * @param dishGuid
	 * @return
	 */
	public boolean isInCart(String dishGuid) {
		return cartItemStore.containsKey(dishGuid);
	}

	/**
	 * 获取购物车中购物项目数量
	 *
	 * @return
	 */
	public int getCount() {
		int count = 0;
		for (Map.Entry <String, Map <String, ICartItem>> entry : cartItemStore.entrySet()) {
			for (Map.Entry <String, ICartItem> tEntry : entry.getValue().entrySet()) {
				count += tEntry.getValue().getCount();
			}
		}
		return count;
	}

	/**
	 * 进入到结算中心的时候，获取被选中的购物项(数据源)
	 *
	 * @return
	 */
	public List <ICartItem> getTotalCartItem() {
		List <ICartItem> items = new ArrayList<>();
		for (Map.Entry <String, Map <String, ICartItem>> entry : cartItemStore.entrySet()) {
			for (Map.Entry <String, ICartItem> tEntry : entry.getValue().entrySet()) {
				items.add(tEntry.getValue());
			}
		}

		Collections.sort(items, new Comparator <ICartItem>() {
			@Override
			public int compare(ICartItem t, ICartItem t1) {
				long time1 = t.getAddTime();
				long time2 = t1.getAddTime();
				if (time1 > time2) {
					return 1;
				} else if (time1 < time2) {
					return -1;
				} else {
					return 0;
				}
			}
		});
		return items;
	}

	/**
	 * 相同guid的购物车项总数量
	 * 
	 * @param dishGuid
	 * @return
	 */
	public int getSameGuidItemCount(String dishGuid) {
		int count = 0;
		if (isInCart(dishGuid)) {
			for (Map.Entry <String, ICartItem> entry : cartItemStore.get(dishGuid).entrySet()) {
				count += entry.getValue().getCount();
			}
		}
		return count;
	}

	/**
	 * 获取单个Item数量
	 * 
	 * @param cartItem
	 * @return
	 */
	public int getItemCount(ICartItem cartItem) {
		String dishGuid = cartItem.getDishGuid();
		int count = 0;
		if (isInCart(dishGuid)) {
			Map <String, ICartItem> map_cartItem = cartItemStore.get(dishGuid);
			ICartItem item = map_cartItem.get(cartItem.getSignID());
			if (item != null) {
				count = item.getCount();
			}
		}
		return count;
	}

	/**
	 * 获取某个购物项目的集合
	 *
	 * @author 陈德基
	 *         create at 2017/4/26 18:43
	 */
	public Map <String, ICartItem> getItems(String dishGuid) {
		return cartItemStore.get(dishGuid);
	}

	/**
	 * 清空购物车
	 */
	public void clear() {
		if (ObjectUtil.checkMap(cartItemStore)) {
			cartItemStore.clear();
			updateAndSend();
		}
	}

	private void VIPCalculate() {
		dishCount = 0;
		vipDishTotalCost = 0;
		if (ObjectUtil.checkMap(cartItemStore)) {
			for (Map.Entry <String, Map <String, ICartItem>> entry : cartItemStore.entrySet()) {
				for (Map.Entry <String, ICartItem> tEntry : entry.getValue().entrySet()) {
					dishCount += tEntry.getValue().getCount();
					vipDishTotalCost += tEntry.getValue().getVIPPrice()
							* tEntry.getValue().getCount();
				}
			}
		}
	}

	/**
	 * 获取某个购物车项的详细信息
	 *
	 * @author 陈德基
	 *         create at 2017/3/21 22:48
	 */
	public ICartItem getItemInfo(ICartItem item) {
		ICartItem resultItem = null;
		if (!ObjectUtil.checkObject(item)) {
			return resultItem;
		}
		if (isInCart(item.getDishGuid())) {
			Map <String, ICartItem> tMap = cartItemStore.get(item.getDishGuid());
			if (!ObjectUtil.checkMap(tMap))
				return resultItem;
			if (tMap.containsKey(item.getSignID())) {
				resultItem = tMap.get(item.getSignID());
			} else {
				for (Map.Entry <String, ICartItem> entry : tMap.entrySet()) {
					resultItem = entry.getValue();
				}
			}
			return resultItem;
		}

		return resultItem;
	}

	/**
	 * 用于直接修改购物车购物项数量的接口
	 *
	 * @author 陈德基
	 *         create at 2016/12/23 10:19
	 */
	public void setItem(ICartItem item) {
		if (!ObjectUtil.checkObject(item)) {
			return;
		}
		ICartItem t = null;
		if (isInCart(item.getDishGuid())) {
			Map <String, ICartItem> tMap = cartItemStore.get(item.getDishGuid());
			if (tMap != null) {
				t = tMap.get(item.getSignID());
				if (t != null) {
					int count = item.getCount();
					if (count <= 0) {
						tMap.remove(t.getSignID());
					} else {
						t.setCount(count);
					}
				} else {
					if (item.getCount() <= 0)
						return;
					item.setAddTime(System.currentTimeMillis());
					tMap.put(item.getSignID(), item);
					t = item;
				}
			} else {
				if (item.getCount() <= 0)
					return;
				item.setAddTime(System.currentTimeMillis());
				tMap = new HashMap<>();
				tMap.put(item.getSignID(), item);
				cartItemStore.put(item.getDishGuid(), tMap);
				t = item;
			}
		} else {
			if (item.getCount() <= 0)
				return;
			item.setAddTime(System.currentTimeMillis());
			Map <String, ICartItem> tMap = new HashMap<>();
			tMap.put(item.getSignID(), item);
			cartItemStore.put(item.getDishGuid(), tMap);
			t = item;
		}
		currentItem = t;
		this.isAdd = true;
		updateAndSend();
	}

	/**
	 * 获取购物车当前的总费用
	 *
	 * @return
	 */
	public String getTotalCost() {
		return NumberUtil.transNumberToDot(dishTotalCost);
	}

	/**
	 * 获取会员价
	 *
	 * @return
	 */
	public String getVipTotalCost() {
		VIPCalculate();
		return NumberUtil.transNumberToDot(vipDishTotalCost);
	}

	@Override
	public void release() {
		this.cartItemStore.clear();
		this.observers.clear();
		this.currentItem = null;
		instance = null;
	}

	/////////////////////////////////////// 以下为旧方法，暂时放着/////////////////////////////////////////////////////////////////////////////

	/**
	 * 对购物车的操作
	 * 只有在缓存中操作
	 */
	private void oldAddItem(ICartItem item, boolean isNeedSyncToServer) {
		if (!ObjectUtil.checkObject(item))
			return;
		ICartItem cartItem = null;
		Logger.d("=====addItem====" + item.getDishGuid());
		if (cartItemStore.containsKey(item.getDishGuid())) {
			Map <String, ICartItem> tMap = cartItemStore.get(item.getDishGuid());
			if (tMap != null) {
				cartItem = tMap.get(item.getSignID());
				if (cartItem != null) {
					int count = cartItem.getCount();
					count++;
					cartItem.setCount(count);
				} else {
					item.setAddTime(System.currentTimeMillis());
					item.setCount(1);
					tMap.put(item.getSignID(), item);
					cartItem = item;
				}
			} else {
				item.setAddTime(System.currentTimeMillis());
				tMap = new HashMap<>();
				item.setCount(1);
				tMap.put(item.getSignID(), item);
				cartItemStore.put(item.getDishGuid(), tMap);
				cartItem = item;
			}
		} else {
			item.setAddTime(System.currentTimeMillis());
			item.setCount(1);
			Map <String, ICartItem> tMap = new HashMap<>();
			tMap.put(item.getSignID(), item);
			cartItemStore.put(item.getDishGuid(), tMap);
			cartItem = item;
		}
		currentItem = cartItem;
		this.isAdd = true;
		updateAndSend();

		// 同步到服务端
		if (isNeedSyncToServer) {
			BillDish billDish = convertToBillDish(item);
			billDish.setAmount(1);
			WebSocketTool.getInstance().shopCart(billDish);
		}

	}

	/**
	 * 删减购物项的数量
	 *
	 * @param dishGuid
	 */
	private void reduceItemCount(String dishGuid, boolean isNeedSyncToServer) {
		Logger.i("===reduceItemCount====" + id);
		ICartItem t = null;
		Map <String, ICartItem> map = cartItemStore.get(dishGuid);
		if (!ObjectUtil.checkMap(map))
			return;
		for (Map.Entry <String, ICartItem> entry : map.entrySet()) {
			t = entry.getValue();
		}
		if (t == null)
			return;
		int count = t.getCount();
		count--;
		if (count > 0) {
			t.setCount(count);
		} else {
			cartItemStore.get(dishGuid).remove(t.getSignID());
		}
		this.isAdd = false;
		currentItem = t;
		updateAndSend();

		// 同步到服务端
		if (isNeedSyncToServer) {
			BillDish billDish = convertToBillDish(t);
			billDish.setAmount(-1);
			WebSocketTool.getInstance().shopCart(billDish);
		}
	}

	private void oldReduceItemCount(ICartItem item, boolean isNeedSyncToServer) {
		if (!ObjectUtil.checkObject(item))
			return;
		// reduceItemCount(item.getDishGuid());
		ICartItem t = null;
		Map <String, ICartItem> map = cartItemStore.get(item.getDishGuid());
		if (!ObjectUtil.checkMap(map))
			return;
		t = map.get(item.getSignID());
		if (t == null)
			return;
		int count = t.getCount();
		count--;
		if (count <= 0) {
			deleteItem(t);
			return;
		} else {
			t.setCount(count);
		}
		this.isAdd = false;
		this.currentItem = t;
		updateAndSend();

		// 同步到服务端
		if (isNeedSyncToServer) {
			BillDish billDish = convertToBillDish(item);
			billDish.setAmount(-1);
			WebSocketTool.getInstance().shopCart(billDish);
		}
	}

}
